
<template>
    <div>
        <Title :title="title"></Title>
        <div ref="right2" style="height: 150px;margin-top: 20px;"></div>
    </div>
</template>
<script>

import { Bar } from '@antv/g2plot';
import { get } from '@/utils/request';
import moment from 'moment';
import { mapState } from 'vuex';

export default {
    data() {
        return {
            title: '实时颗粒物曲线',
            params: {
                page: 1,
                pageSize: 100,
            },
            // 存放pm25数据
            Pm25Data: [],
            // 创建一个图表实例
            line:null,
        }
    },
    computed: {
        ...mapState(['ed']),
    },
    // 侦听器
    watch:{
        ed:{
            handler(n,o){
                // 根据设备信息重新查询数据 渲染图表
                this.params.engineer_id = n.engineer_id;
                this.params.device_id = n.device_id;
                this.getPm25Data();
                // 根据最新获取的数据重新渲染图表
                this.bar.changeData(this.Pm25Data);
            },
            // 深度侦听
            deep:true,
        }
    },
    methods: {
        // 封装加载条形图函数
        loadLine() {
           this.bar = new Bar(this.$refs.right2, {
                data: this.Pm25Data,
                xField: 'pm25',
                yField: 'insert_time',
                seriesField: 'insert_time',
                yAxis: {
                    label: {
                        style: {
                            fill: "white",
                            fontSize: 12,
                        },
                    }
                },
                legend: {
                    position: 'top-left',
                },
                tooltip: {
                    showMarkers: true,
                    fields: ['pm25'],
                    domStyles: {
                        "g2-tooltip": {
                            background: "#147",
                            color: "white",
                            fontSize: 15,
                        },
                    },
                },
            });
            this.bar.render();
        },
        async getPm25Data() {
            let res = await get('/dashboard/pageQueryTodayData', this.params);
            let pm25 = [];
            res.data.list.forEach((item, index, arr) => {
                arr[0].pm25 = 44;
                item.insert_time = moment(item.insert_time).format('HH:mm');
                //由于查询数据密度密集 需要将数据处理 每隔10条数据取一条数据
                if (index % 10 == 0) {
                    pm25.push(arr[index])
                }
            });
            this.Pm25Data = pm25;
        },
    },
    async mounted() {
        await this.getPm25Data();
        this.loadLine();
    },
}
</script>
<style lang="less" scoped></style>